#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class StdClassProvider : CY.GFive.Core.Providers.DALProvider.IStdClassProvider
	{
		#region Sql
		private static readonly string SqlSelectStdClass = "SELECT [Id], [StdCode],[ClassCode],[State] FROM [StdClass] WHERE  [Id]=@Id";

		private static readonly string SqlInsertStdClass = "INSERT INTO [StdClass]([StdCode],[ClassCode],[State]) values(@StdCode,@ClassCode,@State);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateStdClass = "UPDATE [StdClass] SET [StdCode] = @StdCode,[ClassCode] = @ClassCode,[State] = @State WHERE  [Id]=@Id";

		private static readonly string SqlDeleteStdClass = "DELETE [StdClass] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllStdClass = "SELECT [Id], [StdCode],[ClassCode],[State] FROM [StdClass]";
		#endregion

		#region IStdClassProvider Members
		public Core.Business.StdClass Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectStdClass);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.StdClass stdClass = new Core.Business.StdClass();

				if (!reader.IsDBNull(0)) stdClass.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) stdClass.StdCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) stdClass.ClassCode = reader.GetString(2);
				if (!reader.IsDBNull(3)) stdClass.State = reader.GetBoolean(3);

				return stdClass;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.StdClass stdClass)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdCode", SqlDbType.NVarChar, stdClass.StdCode);
			sql.AddParameter("@ClassCode", SqlDbType.NVarChar, stdClass.ClassCode);
			sql.AddParameter("@State", SqlDbType.Bit, stdClass.State);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertStdClass);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) stdClass.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.StdClass stdClass)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdCode", SqlDbType.NVarChar, stdClass.StdCode);
			sql.AddParameter("@ClassCode", SqlDbType.NVarChar, stdClass.ClassCode);
			sql.AddParameter("@State", SqlDbType.Bit, stdClass.State);
			sql.AddParameter("@Id", SqlDbType.Int, stdClass.Id);
			sql.ExecuteSql(SqlUpdateStdClass);
		}

		public void Delete(Core.Business.StdClass stdClass)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, stdClass.Id);
			sql.ExecuteSql(SqlDeleteStdClass);
		}

		public IList<Core.Business.StdClass> GetAllStdClass()
		{
			IList<Core.Business.StdClass> stdClasslist = new List<Core.Business.StdClass>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllStdClass);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.StdClass stdClass = new Core.Business.StdClass();

					if (!reader.IsDBNull(0)) stdClass.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) stdClass.StdCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) stdClass.ClassCode = reader.GetString(2);
					if (!reader.IsDBNull(3)) stdClass.State = reader.GetBoolean(3);

					stdClass.MarkOld();
					stdClasslist.Add(stdClass);
				}
				reader.Close();
			}
			return stdClasslist;
		}
		#endregion
	}
}
